package by.android.khan;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import by.android.khan.authorization.OAuthHelper;
import by.android.khan.R;
import by.android.khan.R.layout;
import by.android.khan.R.menu;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class KhanLoginActivity extends Activity {

	private WebView mWebView;
	private static final String TAG = KhanLoginActivity.class.getSimpleName();
	@SuppressWarnings("deprecation")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_khan_login);
		mWebView = (WebView)findViewById(R.id.login_webView);
	    mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
	    mWebView.setWebViewClient(new KhanWebViewClient());
		mWebView.getSettings().setPluginsEnabled(true);
	    mWebView.loadUrl(OAuthHelper.getAuthURL());
        
	}

	private class KhanWebViewClient extends WebViewClient {

        /**
         * Instantiates a new twitter login web view client.
         *
         * @param dialog the dialog
         * @param runnable the runnable
         */
                public KhanWebViewClient() {
                        super();
                        
                }

                @Override
                public void onPageStarted(WebView view, String url, Bitmap favicon) {
                        Log.d(TAG, "page started " + url);
                        showLoading();
                        if (OAuthHelper.isRedirect(url)) {
                                view.setVisibility(View.INVISIBLE);
                                Log.d(TAG, "Parsing url" + url);
                        }
                }

                
                
                private void showLoading() {
                        findViewById(R.id.login_loading).setVisibility(View.VISIBLE);
                }

                /* (non-Javadoc)
                 * @see android.webkit.WebViewClient#shouldOverrideUrlLoading(android.webkit.WebView, java.lang.String)
                 */
                @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                        Log.d(TAG, "overr " + url);
                if (OAuthHelper.isRedirect(url)) {
                        Log.d(TAG, "overr redr");
                        view.setVisibility(View.INVISIBLE);
                        Log.d(TAG, "Parsing url" + url);
                        return true;
                } else {
                         //view.loadUrl(url);
                         return false;
                }
                }

                @Override
                public void onReceivedError(WebView view, int errorCode,
                                String description, String failingUrl) {
                        super.onReceivedError(view, errorCode, description, failingUrl);
                        Log.d(TAG, "error " + failingUrl);
                }
                
                @Override
                public void onPageFinished(WebView view, String url) {
                        super.onPageFinished(view, url);
                        Log.d(TAG, "finish " + url);
                        if (url.contains("&amp;")) {
                                url = url.replace("&amp;", "&");
                                Log.d(TAG, "overr after replace " + url);
                                view.loadUrl(url);
                                return;
                        }
                        if (!OAuthHelper.proceedRedirectURL(KhanLoginActivity.this, url, success)) {
                                hideLoading();
                        }
                }

                private void hideLoading() {
                        findViewById(R.id.login_loading).setVisibility(View.GONE);
                }

        }
                 
    
    private Runnable success = new Runnable() {
                
                @Override
                public void run() {
                        setResult(RESULT_OK);
                        KhanLoginActivity.this.finish();
                }
                
        };
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_user_login, menu);
		return true;
	}

}
